package com.jdrx.ocp.api;

import com.jdrx.ocp.beans.dto.dept.QueryOpenDeptDTO;
import com.jdrx.ocp.beans.dto.role.QueryOpenRoleDTO;
import com.jdrx.ocp.beans.dto.user.QueryOpenUserDTO;
import com.jdrx.ocp.beans.dto.user.QueryUserResourceDTO;
import com.jdrx.ocp.service.OpenService;
import com.jdrx.ocp.service.UserService;
import com.jdrx.ocp.service.UserServiceClickService;
import com.jdrx.ocp.utils.Constants;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_PLATFORM_ID_FEILD;
import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

/**
 * @Author: Huangxg
 * @Date: 2018/11/22 14:42
 */
@RestController
@RequestMapping("/oapi/0/")
@Api(value = "开放接口" , description = "对外开放查询接口")
public class OpenApi {

    @Autowired
    OpenService openService;
    @Autowired
    UserService userService;
    @Autowired
    UserServiceClickService userServiceClickService;

    @RequestMapping(value = "queryDept", method = RequestMethod.POST)
    @ApiOperation(value = "部门查询", notes = "部门查询")
    public ResposeVO queryDept(@RequestBody @Valid QueryOpenDeptDTO queryOpenDeptDTO) throws BizException {
        return ResponseFactory.ok(openService.queryDept(queryOpenDeptDTO));
    }

    @RequestMapping(value = "queryRole", method = RequestMethod.POST)
    @ApiOperation(value = "角色查询", notes = "角色查询")
    public ResposeVO queryRole(@RequestBody @Valid QueryOpenRoleDTO queryOpenRoleDTO) throws BizException {
        return ResponseFactory.ok(openService.queryRole(queryOpenRoleDTO));
    }

    @RequestMapping(value = "queryUser", method = RequestMethod.POST)
    @ApiOperation(value = "用户查询", notes = "用户查询")
    public ResposeVO queryUser(@RequestBody @Valid QueryOpenUserDTO queryOpenUserDTO,
                               @RequestHeader(TRANSPARENT_PLATFORM_ID_FEILD)String prefix) throws BizException {
        return ResponseFactory.ok(openService.queryUser(queryOpenUserDTO , prefix));
    }

    @RequestMapping(value = "queryUserById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID查询用户", notes = "根据ID查询用户")
    public ResposeVO queryUserById(@RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(openService.queryUserById(idDTO.getId()));
    }

    @RequestMapping(value = "getUserResource", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户资源", notes = "获取用户资源")
    public ResposeVO getUserResource(@RequestHeader(TRANSPARENT_USERID_FEILD) Long userId,
                                     @RequestHeader(TRANSPARENT_PLATFORM_ID_FEILD) String pId ,
                                     @RequestHeader(value = Constants.TENANT_ID , required = false , defaultValue = "") String tid) throws BizException {
        QueryUserResourceDTO queryUserResourceDTO = new QueryUserResourceDTO();
        queryUserResourceDTO.setPrefix(pId);
        queryUserResourceDTO.setUserId(userId);
        return ResponseFactory.ok(openService.getUserResource(queryUserResourceDTO , tid));
    }

    @RequestMapping(value = "queryUserByToken", method = RequestMethod.POST)
    @ApiOperation(value = "根据TOKEN查询用户", notes = "根据TOKEN查询用户")
    public ResposeVO queryUserByToken(@RequestHeader(TRANSPARENT_USERID_FEILD)Long userId) throws BizException {
        return ResponseFactory.ok(openService.queryUserById(userId));
    }

    @RequestMapping(value = "getUserRoleResource", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户角色和资源", notes = "获取用户角色和资源")
    public ResposeVO getUserRoleResource(@RequestHeader(TRANSPARENT_USERID_FEILD)Long userId,
                                         @RequestHeader(TRANSPARENT_PLATFORM_ID_FEILD)String prefix) throws BizException {
        return ResponseFactory.ok(userService.getUserRoleResource(userId,prefix));
    }

}
